package com.yystudy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yystudy.model.system.SysLoginLog;
import com.yystudy.model.vo.SysLoginLogQueryVo;
import com.yystudy.system.mapper.SysLoginLogMapper;
import com.yystudy.system.service.LoginLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginLogServiceImpl implements LoginLogService {

    @Autowired
    private SysLoginLogMapper sysLoginLogMapper;
    @Override
    public void recordLoginLog(String username, Integer status, String ipaddr, String message) {

        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUsername(username);
        sysLoginLog.setStatus(status);
        sysLoginLog.setIpaddr(ipaddr);
        sysLoginLog.setMsg(message);

        //登录日志写入写入数据库中
        sysLoginLogMapper.insert(sysLoginLog);
    }

    /**
     * 条件分页查询登录日志
     * @param page
     * @param limie
     * @param sysLoginLogQueryVo
     * @return
     */
    @Override
    public IPage<SysLoginLog> selectPage(Long page, Long limie, SysLoginLogQueryVo sysLoginLogQueryVo) {
        Page<SysLoginLog> pageParam = new Page<>(page, limie);

        LambdaQueryWrapper<SysLoginLog> lqw = new LambdaQueryWrapper<>();
        String username = sysLoginLogQueryVo.getUsername();
        String createTimeBegin = sysLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginLogQueryVo.getCreateTimeEnd();

        if (!StringUtils.isEmpty(username)){
            lqw.like(SysLoginLog::getUsername,username);
        }
        if (!StringUtils.isEmpty(createTimeBegin)){
            lqw.ge(SysLoginLog::getCreateTime,createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeEnd)){
            lqw.le(SysLoginLog::getCreateTime,createTimeEnd);
        }
        lqw.orderByDesc(SysLoginLog::getCreateTime);

        IPage<SysLoginLog> sysLoginLogPage = sysLoginLogMapper.selectPage(pageParam, lqw);

        return sysLoginLogPage;
    }


}
